package com.lingnan.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lingnan.dto.JobViewRequest;
import com.lingnan.entity.vo.Result;
import com.lingnan.service.JobViewRecordService;
import com.lingnan.vo.JobViewRecordVO;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

// JobViewController.java
@RestController
@RequestMapping("/job/job-view")
@Slf4j
public class JobViewController {
    
    @Resource
    private JobViewRecordService jobViewRecordService;

    /**
     * 记录职位浏览,职位浏览数加1(不会重复)，同时插入职位浏览表
     * @param request
     * @param httpRequest
     * @return
     */
    @PostMapping("/record")
    public Result<Boolean> recordJobView(@RequestBody JobViewRequest request, HttpServletRequest httpRequest) {
        try {
            String ipAddress = getClientIpAddress(httpRequest);
            String userAgent = httpRequest.getHeader("User-Agent");
            
            boolean result = jobViewRecordService.recordJobView(
                request.getUserId(), 
                request.getJobId(), 
                ipAddress, 
                userAgent
            );
            
            return Result.ok(result);
        } catch (Exception e) {
            log.error("记录职位浏览失败", e);
            return Result.fail("记录浏览失败");
        }
    }

    /**
     * 检查是否已浏览
     * @param userId
     * @param jobId
     * @return
     */
    @GetMapping("/check")
    public Result<Boolean> checkUserViewedJob(@RequestParam("userId") Long userId, @RequestParam("jobId") Long jobId) {
        boolean viewed = jobViewRecordService.hasUserViewedJob(userId, jobId);
        return Result.ok(viewed);
    }

    /**
     * 获取用户浏览记录
     * @param userId
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/user-records")
    public Result<IPage<JobViewRecordVO>> getUserViewRecords(
            @RequestParam("userId") Long userId,
            @RequestParam(value = "current",defaultValue = "1") int current,
            @RequestParam(value = "size",defaultValue = "10") int size) {
        
        IPage<JobViewRecordVO> records = jobViewRecordService.getUserViewRecords(userId, current, size);
        return Result.ok(records);
    }

    /**
     * 获取职位真实浏览用户数
     * @param jobId
     * @return
     */
    @GetMapping("/job-view-count")
    public Result<Integer> getJobViewUserCount(@RequestParam("jobId") Long jobId) {
        int count = jobViewRecordService.getJobViewUserCount(jobId);
        return Result.ok(count);
    }
    
    /**
     * 获取客户端IP地址
     */
    private String getClientIpAddress(HttpServletRequest request) {
        String xForwardedFor = request.getHeader("X-Forwarded-For");
        if (xForwardedFor != null && !xForwardedFor.isEmpty() && !"unknown".equalsIgnoreCase(xForwardedFor)) {
            return xForwardedFor.split(",")[0].trim();
        }
        
        String xRealIp = request.getHeader("X-Real-IP");
        if (xRealIp != null && !xRealIp.isEmpty() && !"unknown".equalsIgnoreCase(xRealIp)) {
            return xRealIp;
        }
        
        return request.getRemoteAddr();
    }
}

